package cn.ddiancan.xddcloud.common.context;

import java.util.Optional;

import cn.ddiancan.xddcloud.common.entity.UserVO;

public class RequestContext<T> {

    private T currentUser;

    public RequestContext() {
    }

    public RequestContext(T currentUser) {
        this.currentUser = currentUser;
    }


    public static RequestContext<UserVO> getRequestContext(boolean allowdEmpty) {
        RequestContext<UserVO> requestContext = RequestContextManager.getRequestContext();
        Optional<RequestContext<UserVO>> optionalRequestContext = Optional.ofNullable(requestContext);
        return optionalRequestContext.orElse(allowdEmpty ? new RequestContext<>() : null);
    }

    public static UserVO getCurrentContextUser() {
        RequestContext<UserVO> requestContext = getRequestContext(true);
        return requestContext.getCurrentUser();
    }

    public T getCurrentUser() {
        return currentUser;
    }

    public void setCurrentUser(T currentUser) {
        this.currentUser = currentUser;
    }
}
